#include "stdafx.h"
#include "ServerSessionManager.h"
#include "ServerSession.h"
#include <assert.h>

ServerSessionManager::ServerSessionManager()
{
	m_pMasterServer	= NULL;
	m_pGameDBProxy = NULL;
}

ServerSessionManager::~ServerSessionManager()
{
}

VOID ServerSessionManager::AddServer( ServerSession * pServer )
{
	assert( FindServer( pServer->GetSessionIndex() ) == NULL );
	m_mapSessions.insert( SERVER_SESSION_PAIR( pServer->GetSessionIndex(), pServer ) );

	switch( pServer->GetServerType() )
	{
	case MASTER_SERVER:
		m_pMasterServer = pServer;	break;
	case GAME_DBPROXY:
		m_pGameDBProxy = pServer;	break;
	}
}

VOID ServerSessionManager::RemoveServer( DWORD dwSessionIndex )
{
	SERVER_SESSION_MAP_ITER it = m_mapSessions.find( dwSessionIndex );
	assert( it != m_mapSessions.end() );
	ServerSession *pServer = it->second;
	m_mapSessions.erase( it );

	switch( pServer->GetServerType() )
	{
	case MASTER_SERVER:
		m_pMasterServer = NULL;		break;
	case GAME_DBPROXY:
		m_pGameDBProxy = NULL;		break;
	}
}

ServerSession *	ServerSessionManager::FindServer( DWORD dwSessionIndex )
{
	SERVER_SESSION_MAP_ITER it = m_mapSessions.find( dwSessionIndex );

	if( it == m_mapSessions.end() ) return NULL;

	return it->second;
}

VOID ServerSessionManager::Update()
{
	SERVER_SESSION_MAP_ITER it;

	for( it = m_mapSessions.begin(); it != m_mapSessions.end(); ++it )
	{
		it->second->Update();
	}
}

ServerSession* ServerSessionManager::GetGuildServer()
{
	SERVER_SESSION_MAP_ITER it;

	for( it = m_mapSessions.begin(); it != m_mapSessions.end(); ++it )
	{
		if( it->second->GetServerType() == GUILD_SERVER )
		{
			return it->second;
		}
	}

	return NULL;
}
